<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    function Person(name, age) {
        //禁止当成普通函数
        // if (!new.target) {
        //     throw new Error("Person 是一个构造函数，不能当作普通函数使用");
        // }
        //如何实现，当作普通函数使用时，也可以返回对象
        if (!new.target) {
            return new Person(name, age);
        }

        //给Person中的name属性赋值为name形参中的值
        this.name = name;
        this.age = age;

        this.say = function () {
            console.log(this.name)
            console.log(this.age);
        }
    }
    //挂载在函数对象上
    Person.money = 100;
    Person.cost = function () {
        console.log(this.money--);
    }

    Person.cost();
    console.log(Person.money);

    let p1 = new Person("haha", 18);
    let p2 = new Person("张三", 28);
    console.log(p1);
    p1.say();
    console.log(p2);
    p2.say();
    // Person();
    let a1 = [1, 2, 3];
    let a2 = Array(4, 5, 6);
    let a3 = new Array(7, 8, 9);
    console.log(a1, a2, a3);
    let p3 = Person("hehe", 20);
    console.log(p3);

    console.log(p1.say === p2.say);

    console.dir(Person);


</script>
</body>
</html>